package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.kpx.app.R;
import com.kpx.app.adapter.MessageAdapter;
import com.kpx.app.adapter.MsgShowAdapter;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.MessageBean;
import com.kpx.app.bean.MessageMainBean;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/11.
 */

public class ActivityMessageShow extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.lv_show)
    ListView lvShow;
    @BindView(R.id.out_view)
    XRefreshView outView;
    @BindView(R.id.txt_empty_mention)
    TextView txtEmptyMention;
    private MsgShowAdapter adapter;
    private List<MessageBean> messageslist = new ArrayList<>();
    private boolean isRefresh;
    private int pageNow= 1;
    private int totalPage;
    public static int msgType;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_msg_show);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "消息列表");
        UIUtils.initRefreshView(outView);
        getMessage();// 获取数据
    }

    @Override
    public void initData() {
        outView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                pageNow = 1;
                getMessage();// 获取数据
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isRefresh = false;
                if (pageNow >= totalPage) {
                    ToastUtil.getInstance().showToast(ActivityMessageShow.this, "没有更多了");
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            outView.stopLoadMore();
                        }
                    }, 600);
                } else {
                    pageNow++;
                    getMessage();// 获取数据
                }
            }
        });
        adapter = new MsgShowAdapter(ActivityMessageShow.this, messageslist, R.layout.item_message_show);
        lvShow.setAdapter(adapter);
    }

    /**
     * 获取消息列表
     */
    private void getMessage() {
        MyHashMap map = new MyHashMap();
        map.put("pageIndex", pageNow + "");
        map.put("pageSize", "20");
//        map.put("msgType", msgType + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (isRefresh) {
                    outView.stopRefresh();
                } else {
                    outView.stopLoadMore();
                }
                if (result.getCode() == 0) {
                    if (result.getData() != null) {
                        if (isRefresh) {
                            messageslist.clear();
                        }
                        messageslist.addAll((ArrayList<MessageBean>) result.getData());
                        if (messageslist.size() > 0) {
                            adapter.setData(messageslist);
                            txtEmptyMention.setVisibility(View.GONE);
                        } else {
                            txtEmptyMention.setVisibility(View.VISIBLE);
                        }

                        totalPage = result.getPage().getPageSize();
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityMessageShow.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                if (isRefresh) {
                    outView.stopRefresh();
                } else {
                    outView.stopLoadMore();
                }
                ToastUtil.getInstance().showToast(ActivityMessageShow.this, error);
            }
        }, MessageBean.class, HttpDataRequest.TYPE_LIST).POST(HttpDataReqUrl.URL_POST_GET_MESSAGE_SHOW, true, map);
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }
}
